package com.treeroot.orderservice.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treeroot.commonutils.ordervo.PlaneInfoOrder;
import com.treeroot.commonutils.ordervo.UserInfoOrder;
import com.treeroot.commonutils.utils.OrderNoUtil;
import com.treeroot.orderservice.order.client.PlaneClient;
import com.treeroot.orderservice.order.client.UserClient;
import com.treeroot.orderservice.order.entity.PcOrder;
import com.treeroot.orderservice.order.mapper.PcOrderMapper;
import com.treeroot.orderservice.order.service.PcOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author TreeRoot
 * @since 2022-05-18
 */
@Service
@Transactional
public class PcOrderServiceImpl extends ServiceImpl<PcOrderMapper, PcOrder> implements PcOrderService {

    @Autowired
    private PlaneClient planeClient;
    @Autowired
    private UserClient userClient;

    /**
     * 订单分页
     * @param current
     * @param limit
     * @return
     */
    @Override
    public Map getOrderList(long current, long limit) {
    Page<PcOrder> orderPage =new Page<>(current,limit);
    baseMapper.selectPage(orderPage,null);
        List<PcOrder> records = orderPage.getRecords();
        long total = orderPage.getTotal();
        Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return map;
    }

    /**
     * 通过订单id得到一条订单信息
     * @param orderId
     * @return
     */
    @Override
    public PcOrder getOrderInfo(String orderId) {
        QueryWrapper<PcOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        PcOrder orderNo = baseMapper.selectOne(wrapper);
        return  orderNo;
    }

    /**
     * 订单条件分页
     * @param current
     * @param limit
     * @param order
     * @return
     */
    @Override
    public Map getQueryOrderList(long current, long limit, PcOrder order) {
        Page<PcOrder> orderPage = new Page<>(current,limit);
        LambdaQueryWrapper<PcOrder> wrapper =new QueryWrapper<PcOrder>().lambda()
                .eq(!StringUtils.isEmpty(order.getOrderNo()),
                        PcOrder::getOrderNo,
                        order.getOrderNo()
                );

        baseMapper.selectPage(orderPage, wrapper);
        long total = orderPage.getTotal();
        List<PcOrder> records = orderPage.getRecords();
        Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return map;
    }


    @Override
    public String createBaseOrder(String planeId,String planeType, String userId) {


        /**
         * 远程调用用户信息  根据用户id
         */
        UserInfoOrder userInfoOrder = userClient.getUserInfoOrder(userId);

        /**
         * 远程调用航班信息根据航班id   【和 航班类型】
         */
        PlaneInfoOrder planeInfoOrder = planeClient.getPlaneInfoOrder(planeId,planeType);

        /**
         * 建Order对象，向order对象里面设置需要数据
         */
        PcOrder order = new PcOrder();

        order.setOrderNo(OrderNoUtil.getOrderNo()); //工具栏生成的订单号
        /**
         *  远程获得的机票的信息
         */
        order.setPlaneId(planeId);
        order.setFlightStartTime(planeInfoOrder.getFlightStartTime());
        order.setFlightEndTime(planeInfoOrder.getFlightEndTime());
        order.setStartCity(planeInfoOrder.getStartCity());
        order.setEndCity(planeInfoOrder.getEndCity());
        order.setSeating(planeInfoOrder.getSeating());
        order.setPlaneType(planeInfoOrder.getPlaneType());
        String grade = planeInfoOrder.getGrade();

//       根据舱位等级 动态控制价格
        if (grade.equals(2)){
            order.setTotalFee(planeInfoOrder.getPrice()+500);
        } else if (grade.equals(3)) {
            order.setTotalFee(planeInfoOrder.getPrice()+1000);

        }else
            order.setTotalFee(planeInfoOrder.getPrice());

        /**
         *  远程获得的机票的信息
         */

        order.setUserId(userId);
        order.setName(userInfoOrder.getName());
        order.setPhone(userInfoOrder.getPhone());
        order.setStatus(0);
        order.setPayType(2);
        /**
         *  插入到 订单表
         */
        baseMapper.insert(order);

        return order.getOrderNo();
    }

}

/**
 * 用支付宝沙盒的方式去进行支付
 *
 * @GetMapping("/pay")
 * @ResponseBody 需要传回来的值
 * public R alipay(
 * @PathVariable String orderNo,
 * @PathVariable String planeId,
 * @PathVariable String startCity,
 * @PathVariable String endCity,
 * @PathVariable Date flightStartTime,
 * @PathVariable Date flightEndTime,
 * @PathVariable Integer seating,
 * @PathVariable String userId,
 * @PathVariable String name,
 * @PathVariable String phone,
 * @PathVariable Integer price) throws AlipayApiException {
 * <p>
 * PcOrder alipayBean = new PcOrder();
 * alipayBean.setOrderNo(orderNo);
 * alipayBean.setPlaneId(planeId);
 * alipayBean.setStartCity(startCity);
 * alipayBean.setEndCity(endCity);
 * alipayBean.setFlightStartTime(flightStartTime);
 * alipayBean.setFlightEndTime(flightEndTime);
 * alipayBean.setSeating(seating);
 * alipayBean.setUserId(userId);
 * alipayBean.setName(name);
 * alipayBean.setPhone(phone);
 * alipayBean.setTotalFee(price);
 * <p>
 * <p>
 * String flag = payLogService.aliPay(alipayBean);
 * <p>
 * if (!(flag == null)) {
 * System.out.println("失败");
 * } else {
 * //远程调用航班信息根据航班id
 * System.out.println("支付成功");
 * //            修改支付状态
 * alipayBean.setStatus(1);
 * payOrderService.updateById(alipayBean);
 * <p>
 * this.flag.updatePlaneByIdOrder(planeId);
 * }
 * return R.ok().data("data", flag);
 * }
 */